package tdh.xt.service.impl;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tdh.xt.common.utils.CommonUtil;
import tdh.xt.dao.UserDao;
import tdh.xt.entity.Translater;
import tdh.xt.entity.po.User;
import tdh.xt.entity.vo.Page;
import tdh.xt.service.UserService;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource(name = "userDao")
    UserDao userDao;

    @Autowired
    Translater translater;

    Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    @Transactional(transactionManager = "transactionManager")
    public User login(String yhid, String yhkl) {
        User dbUser = userDao.getUserByYhid(yhid);
        if (dbUser == null || !dbUser.getYhkl().equals(yhkl)) {
            return null;
        }
        translater.translate(dbUser);
        return dbUser;
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public Page<User> search(String id, Page<User> page) {
        List<User> users = StringUtils.isEmpty(id) ? userDao.getUserList(page) : userDao.getUsersByYhidOrYhxm(id, page);
        Collections.sort(users);
        translater.translate(users);
        page.setData(users);
        Map<String, Object> map = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(id)) {
            map.put("yhxm like", id);
            map.put("or yhid", id);
        }
        page.setTotalCount(getTotalPageCount(page.getPageSize(), map));
        return page;
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public int deleteByYhdm(String yhdm) {
        return userDao.deleteByYhdm(yhdm);
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public Page<User> getUserListFromBmdmWithPage(String bmdm, Page<User> page) {
        List<User> users = userDao.getUsersByBmdm(bmdm, page);
        Collections.sort(users);
        translater.translate(users);
        page.setData(users);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("yhbm", bmdm);
        page.setTotalCount(getTotalPageCount(page.getPageSize(), map));
        return page;
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public User getUserFromYhdm(String yhdm) {
        if (StringUtils.isEmpty(yhdm)) {
            return null;
        }
        return userDao.getUserByYhdm(yhdm);
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public boolean hasYhdm(String yhdm) {
        return getUserFromYhdm(yhdm) != null;
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public int insert(User user) {
        return userDao.insert(user);
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public int updateByYhdm(User user) {
        return userDao.updateByYhdm(user);
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public Page<User> getUserList(Page<User> page) {
        List<User> users = userDao.getUserList(page);
        Collections.sort(users);
        translater.translate(users);
        page.setData(users);
        page.setTotalCount(getTotalPageCount(page.getPageSize(), new HashMap<String, Object>()));
        return page;
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public int getTotalPageCount(Integer pageSize, Map<String, Object> map) {
        return userDao.getTotalPageCount(pageSize, map);
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public int saveOrUpdate(User user) {
        if (hasYhdm(user.getYhdm())) {
            return userDao.updateByYhdm(user);
        }
        return userDao.insert(user);
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public String getGridUserList(Page<User> page) {
        Page<User> userList = getUserList(page);
        return CommonUtil.getXmlStringFromPageUser(userList);
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public String gridSearch(String id, Page<User> page) {
        Page<User> userList = search(id, page);
        return CommonUtil.getXmlStringFromPageUser(userList);
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public String getGridUserListFromBmdmWithPage(String bmdm, Page<User> page) {
        Page<User> userList = getUserListFromBmdmWithPage(bmdm, page);
        return CommonUtil.getXmlStringFromPageUser(userList);
    }

    @Override
    @Transactional(transactionManager = "transactionManager")
    public String getXmlUserFromYhdm(String yhdm) {
        User user = getUserFromYhdm(yhdm);
        return CommonUtil.getXmlStringFromUser(user);
    }
}
